package com.ynet.hmds28.service.controller;

import java.util.concurrent.Semaphore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.taobao.pandora.boot.loader.util.StringUtils;
import com.ynet.hm.user.domain.UserBean;
import com.ynet.hm.user.service.QueryUserService;
import com.ynet.hm.utils.config.Result;
import com.ynet.hm.utils.config.ResultUtil;
import com.ynet.hm.utils.enums.ExceptionEnum;
import com.ynet.hm.utils.exception.MyException;
import com.ynet.hmds28.service.config.SemaphoreConfig;

/**
 * Created by chengcaiyi on 2017/12/11.
 */
@RestController
public class UserInfoController {


    @Autowired
    private QueryUserService queryUserService;
    @Autowired
    private SemaphoreConfig semaphoreConfig;
    
    @RequestMapping(value = "/api/user/{id}", method = RequestMethod.GET)
	public Result<?> findUerInfoById(@PathVariable("id") String userId){
    	
    	return queryUserService.findUerInfoById(userId);
    	
    }
    
    @RequestMapping(value = "/api/user/login", method = RequestMethod.POST)
	public Result<?> findUerByNameAndPassword(HttpServletRequest request){
    	
    	Semaphore semaphore = semaphoreConfig.getSemaphore();
    	
    	int availablePermits=semaphore.availablePermits();//可用资源数
    	
    	if(availablePermits>0){
            System.out.println("抢到资源");
        }else{
            System.out.println("资源已被占用，稍后再试");
            return ResultUtil.error(ExceptionEnum.QPS_IS_ERROR);
        }
    	UserBean user = null;
    	try {
            semaphore.acquire(1);  //请求占用一个资源
            System.out.println("资源正在被使用");
            
            String userName = request.getParameter("userName");
        	String password = request.getParameter("password");
        	
        	if(StringUtils.isEmpty(userName)){
        		throw new MyException(ExceptionEnum.USERNAME_IS_NULL);
        	}
        	if(StringUtils.isEmpty(password)){
        		throw new MyException(ExceptionEnum.PASSWORD_IS_NULL);
        	}
        	
        	HttpSession session = request.getSession();
        	user = queryUserService.findUerByNameAndPassword(userName,password);
        	session.setAttribute("userInfo", user);
        	Thread.sleep(30000);//放大资源占用时间，便于观察
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally{
            semaphore.release(1);//释放一个资源
        }
    	return ResultUtil.success(user);
    	
    }
    
    @RequestMapping(value = "/api/user/registe", method = RequestMethod.POST)
	public Result<?> registe(HttpServletRequest request){
    	
    	String userName = request.getParameter("userName");
    	String password = request.getParameter("password");
    	
    	if(StringUtils.isEmpty(userName)){
    		throw new MyException(ExceptionEnum.USERNAME_IS_NULL);
    	}
    	if(StringUtils.isEmpty(password)){
    		throw new MyException(ExceptionEnum.PASSWORD_IS_NULL);
    	}
    	
    	return queryUserService.registe(userName, password);
    	
    }
    
    @RequestMapping(value = "/api/user/modify", method = RequestMethod.POST)
	public Result<?> modifyUserInfo(HttpServletRequest request){
    	
    	String password = request.getParameter("password");
    	String mobile = request.getParameter("mobile");
    	
    	if(StringUtils.isEmpty(password)){
    		throw new MyException(ExceptionEnum.PASSWORD_IS_NULL);
    	}
    	
    	HttpSession session = request.getSession();
    	UserBean user = (UserBean) session.getAttribute("userInfo");
    	
    	return queryUserService.modifyUserInfo(user.getUserid(), mobile, password);
    	
    }
    
}
